package indi.kider.server.applet.service;

import indi.kider.common.constant.AppUserType;
import indi.kider.common.constant.HouseReserveCons;
import indi.kider.common.constant.HouseStateCons;
import indi.kider.common.constant.TenantSexTypeCons;
import indi.kider.common.model.apartment.Community;
import indi.kider.common.model.apartment.Houses;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.employee.Enterprise;
import indi.kider.common.model.employee.Tenant;
import indi.kider.common.model.woniubus.TenantInHouse;
import indi.kider.common.model.woniubus.TenantVo;
import indi.kider.server.apartment.mapper.CommunityMapper;
import indi.kider.server.apartment.mapper.HouseMapper;
import indi.kider.common.model.apartment.HouseApplet;
import indi.kider.server.apartment.mapper.HousesMapper;
import indi.kider.server.applet.model.User;
import indi.kider.server.applet.util.DateTool;
import indi.kider.server.employee.mapper.EmployeeMapper;
import indi.kider.server.employee.mapper.EnterpriseMapper;
import indi.kider.server.employee.mapper.TenantInHouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.model.base.CoreException;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@SuppressWarnings("ALL")
@Service
public class HouseServiceImpl implements HouseService {

    @Autowired
    private CommunityMapper communityMapper;

    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private HousesMapper housesMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private TenantInHouseMapper tenantInHouseMapper;

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Autowired
    private SmsService smsService;

    @Autowired
    private RentService rentService;

    @Override
    public List<Community> getCommunity() throws Exception {
        return communityMapper.selectAll();
    }

    @Override
    public List<Map> getHouse(HouseApplet houseApplet , User user) throws Exception {
        //租客
        if(AppUserType.TENANT == user.getType() || AppUserType.FRIEND == user.getType()){
            //houseApplet.setTenantId(user.getId());
            //房東
        }else if(AppUserType.LANDLORD == user.getType()){
            throw new Exception("暂不支持房东登陆");
            //工人
        }else if(AppUserType.WORKER == user.getType()){
            houseApplet.setWorkId(String.valueOf(user.getId()));
            //houseApplet.setWaiter(user.getId());
            //服务人员
        }else if(AppUserType.WAITER == user.getType()){
            //houseApplet.setWaiter(user.getId());
            //合作企業
        }else if(AppUserType.COOPERATE == user.getType()){
            houseApplet.setEmployeePartId(String.valueOf(user.getId()));
        }
        return houseMapper.getHouse(houseApplet);
    }

    @Override
    public String houseIn(HouseApplet houseApplet, User user) throws Exception {
        if(houseApplet != null && houseApplet.getTenantinhouseId() != null){
            TenantInHouse tenantInHouse = new TenantInHouse();
            tenantInHouse.setId(houseApplet.getTenantinhouseId());
            tenantInHouse.setOpId1(user.getId());
            tenantInHouse.setOpTime1(new Date());

            TenantInHouse s = tenantInHouseMapper.selectByPrimaryKey(houseApplet.getTenantinhouseId());
            Houses h = housesMapper.selectByPrimaryKey(Long.valueOf(houseApplet.getHouseId()));
            TenantInHouse t = new TenantInHouse();
            t.setPhone(s.getPhone());
            t.setHouseNum(h.getHouseNumb());
            //生成短信
            if("1".equals(houseApplet.getType())){
                //添加入住记录
                TenantVo tenantVo = new TenantVo();
                tenantVo.setOpInId(s.getOpId1());
                tenantVo.setRentDate(DateTool.changeDate(new Date(),DateTool.FORMAT_COMMON_DATE));
                tenantVo.setExpireDate(DateTool.parserDate(DateTool.getIntervalDate(1,DateTool.FORMAT_COMMON_DATE,tenantVo.getRentDate(),1),DateTool.FORMAT_COMMON_DATE));
                tenantVo.setRemark("小程序注册");
                tenantVo.setHouseId(h.getId());
                tenantVo.setName(s.getTenantName());
                tenantVo.setPriceSet(0F);
                tenantVo.setPhone(s.getPhone());
                tenantVo.setCardId(s.getCardId());
                tenantVo.setSex(houseApplet.getSex());
                tenantVo.setFormCompy(houseApplet.getFormCompy());

                String result = rentService.rentHouse(tenantVo);
                tenantInHouse.setState(2);
                smsService.tenantSuccessSms(t);
            }else if("2".equals(houseApplet.getType())){
                Employee employee = employeeMapper.selectByPrimaryKey(s.getOpId1());
                t.setPartName(employee.getName());
                t.setPartPhone(employee.getPhone());
                tenantInHouse.setState(3);
                smsService.tenantFailSms(t);
            }else{
                throw new Exception("错误的短信类型");
            }
            tenantInHouseMapper.updateByPrimaryKeySelective(tenantInHouse);
            return "ok";
        }
        return "err";
    }

    @Override
    public List<Map> houseInList(User user) throws Exception {
        List<Map> list = tenantInHouseMapper.getHouseInListByEmployeeId(user.getId());
        return list;
    }

    @Override
    public Map<String, Object> getHouseUserCntMap(Long houseId) throws Exception{
        //inSumCnt:3,inManCnt:1,inFemanCnt:2
        if(houseId==null){
            throw new Exception("缺少房间主键");
        }
        Map<String, Object> map =  houseMapper.getHouseUserCntMap(houseId);
        Houses houses = housesMapper.selectByPrimaryKey(houseId);
        if(map != null){
            BigDecimal a  = (BigDecimal)map.get("inSumCnt");
            BigDecimal b  = (BigDecimal)map.get("inManCnt");
            BigDecimal c  = (BigDecimal)map.get("inFemanCnt");
            houses.setInSumCnt(a.intValue());
            houses.setInManCnt(b.intValue());
            houses.setInFemanCnt(c.intValue());
            // 出租类型 1 独租公寓 2群租公寓
            if(houses.getComType()== 2 && houses.getInManCnt()>0 && houses.getInFemanCnt()>0){
                throw  new CoreException("不能男女混居!");
            }
        }else{
            houses.setInSumCnt(0);
            houses.setInManCnt(0);
            houses.setInFemanCnt(0);
        }
        housesMapper.updateByPrimaryKeySelective(houses);
        return map;
    }

    @Override
    public List<Map> getSameHouseTenantInfoList(HouseApplet houseApplet) throws Exception {
        return tenantInHouseMapper.getSameHouseTenantInfoList(houseApplet.getTenantinhouseId());
    }
}